package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
)

//截图的名称
var png string = "/data/1.png"

func hello(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Println(r.URL.Path)
	screencap()
	//这个是直接读取一张图片，返回
	w.Header().Set("Content-Type", "image/png")
	cont, _ := readAll(png)
	w.Write(cont)
	//下面是直接返回一段内容
	//fmt.Fprintf(w, "hello world")
}

//安卓设备截图
func screencap() {
	cmd := exec.Command("screencap", png)
	buf, err := cmd.Output()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(buf))
}

//读文件的方法
func readAll(filePth string) ([]byte, error) {
	f, err := os.Open(filePth)
	defer f.Close()
	if err != nil {
		return nil, err
	}
	return ioutil.ReadAll(f)
}

func main() {
	http.HandleFunc("/", hello)
	err := http.ListenAndServe(":6060", nil)
	if err != nil {
		fmt.Println(err)
	}

}
